<template>
  <div class="broker">
    <h4>经纪人管理</h4>
    <div class="broker-one">
      <p>
        <input
          type="text"
          placeholder="请输入经纪人姓名/手机号"
          v-model="search"
        /><button @click="btnsea">搜索</button>
      </p>
      <p>
        状态：
        <el-select v-model="value" placeholder="请选择" @change="changebroker">
          <el-option label="在职" value="在职"> </el-option>
          <el-option label="离职" value="离职"> </el-option>
        </el-select>
      </p>
      <p><button @click="btnacc">新建</button></p>
      <Acc ref="dialog" @update="Broker()" />
    </div>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column fixed type="index" label="序号" width="180">
      </el-table-column>

      <el-table-column prop="name" label="姓名" width="180"> </el-table-column>

      <el-table-column prop="phone" label="手机号" width="200">
      </el-table-column>

      <el-table-column prop="company" label="公司" width="180">
      </el-table-column>

      <el-table-column prop="newBuil" label="新房" width="180">
      </el-table-column>

      <el-table-column prop="oldBuil" label="二手房" width="180">
      </el-table-column>

      <el-table-column prop="rent" label="租房" width="180"> </el-table-column>

      <el-table-column prop="state" label="状态" width="200"> </el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="emit(scope.row)"
            >编辑</el-button
          >
          <el-button type="text" size="small" @click="btndel(scope)"
            >删除</el-button
          >
          <el-button type="text" size="small">启用</el-button>
          <el-button type="text" size="small">禁用</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage4"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import Acc from "../../components/acc.vue";
export default {
  data() {
    return {
      tableData: [],
      search: "",
      total: null,
      pageIndex: 1,
      pageSize: 5,
      currentPage4: 1,
      value: "",
    };
  },

  components: {
    Acc,
  },

  methods: {
    handleCurrentChange(val) {
      this.pageIndex = val;
      this.Broker(this.pageIndex, this.pageSize);
    },

    handleSizeChange(val) {
      this.pageSize = val;
      this.Broker(this.pageIndex, this.pageSize);
    },

    //分页
    Broker(pageIndex, pageSize) {
      this.$http
        .get("/api/getAgent", {
          params: {
            pageIndex: pageIndex,
            pageSize: pageSize,
          },
        })
        .then((res) => {
          console.log(res);
          this.tableData = res.data.data;
          this.total = res.data.total;
        });
    },

    //新建
    btnacc() {
      this.$refs.dialog.dialogFormVisible = true;
      this.$refs.dialog.form = {
        name: "",
        phone: "",
        company: "",
        newBuil: "",
        oldBuil: "",
        rent: "",
        state: "",
      };
    },

    //编辑
    emit(eccit) {
      this.$refs.dialog.dialogFormVisible = true;
      this.$refs.dialog.form = JSON.parse(JSON.stringify(eccit));
    },

    //搜索
    btnsea() {
      this.$http
        .post("/api/searchAgen", {
          name: this.search,
        })
        .then((res) => {
          console.log(res);
          if (res.data.code === 1) {
            this.$message.success("搜索成功");
            this.tableData = res.data.data;
          }
        });
    },

    //删除
    btndel(scope) {
      this.$http.post("/api/deleteAgent", { id: scope.row.id }).then((res) => {
        console.log(res);
        if (res.data.code === 1) {
          this.$message.success("删除成功");
          this.Broker(this.pageIndex, this.pageSize);
        }
      });
    },

    //状态筛选
    changebroker() {
      console.log(this.value);
      this.$http
        .post("/api/agnetSelect", {
          state: this.value,
        })
        .then((res) => {
          if (res.data.code === 1) {
            this.$message.success(res.data.message);
            this.tableData = res.data.data;
          }
        });
    },
  },

  created() {
    this.Broker(this.pageIndex, this.pageSize);
  },
};
</script>

<style>
h4{
  width: 100%;
  height: 50px;
  line-height: 50px;
}
.broker{
  background: #e6e6e6;
}
.cell {
  text-align: center;
}
.broker-one {
  width: 100%;
  height: 50px;
  display: flex;
  align-items: center;
  background: #fff;
}
.broker-one p input {
  padding: 10px 0;
  border-radius: 5px;
  border: 1px solid #e6e6e6;
}
.broker-one p button {
  margin: 0 5px;
  padding: 9px 15px;
  background: skyblue;
  color: #fff;
  border: none;
}
</style>